package cn.vser.server.utils;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

public class AuthurlUtil {

    public static String getAuthurl(String url, String uid, String authSecret, String sname) throws URISyntaxException, UnsupportedEncodingException {
        URI u = new URI(url);
        String path = u.getPath();
        //System.out.println(path);
        long times = System.currentTimeMillis();
        String rand = String.valueOf(Math.random() * 1000000);
        if (uid == null) uid = "0";
        StringBuilder sb = new StringBuilder();
        sb.append(path).append("-");
        sb.append(times).append("-");
        sb.append(rand).append("-");
        sb.append(uid).append("-");
        sb.append(authSecret);
        StringBuilder auth = new StringBuilder();
        auth.append(times).append("-");
        auth.append(rand).append("-");
        auth.append(uid).append("-");
        auth.append(CodeUtils.md5(sb.toString()));
        if (url.indexOf('?') >= 0) {
            url = url + "&auth=" + auth;
        } else {
            url = url + "?auth=" + auth;
        }
        if (sname != null) url = url + "&sname=" + CodeUtils.bytes2hex(sname.getBytes("utf-8"));
        return url;
    }
    
    public static void main(String[] args) throws URISyntaxException, UnsupportedEncodingException {
        System.out.println(getAuthurl("rtmp://gitee.com/abc", null, "abc", "摄像头"));
    }
    
}
